import numpy as np

# ---- run Test
num: int = 0


def runTest(fun):
    global num
    print('----------------------', num)
    fun()
    num += 1
    print()


def test_broad():
    a = np.array([0.1, 0.2, 0.3, 0.4])
    b = np.array([10, 20, 30, 40])
    c = a * b
    print(c)


def test_broad2():
    a = np.array([[0, 0, 0],
                  [10, 10, 10],
                  [20, 20, 20],
                  [30, 30, 30]])
    # b数组与a数组形状不同
    b = np.array([1, 2, 3])
    print(a + b)


def test_iter():
    a = np.arange(0, 60, 5)
    a = a.reshape(3, 4)
    # 使用nditer迭代器,并使用for进行遍历
    for x in np.nditer(a):
        print(x, end=",")

    print()
    # a的转置数组
    b = a.T
    print(b)
    for x in np.nditer(b):
        print(x, end=",")

    print('\n\n')
    # copy方法生成数组副本
    c = a.T.copy(order='C')
    print(c)
    for x in np.nditer(c):
        print(x, end=", ")


def test_iter_order():
    a = np.arange(0, 60, 5)
    a = a.reshape(3, 4)
    print(a)
    for x in np.nditer(a, order='C'):
        print(x, end=",")
    print('\n', '--------------------')
    for x in np.nditer(a, order='F'):
        print(x, end=",")


def test_op_flags():
    a = np.arange(0, 60, 5)
    a = a.reshape(3, 4)
    print("原数组是:\n", a)
    for x in np.nditer(a, op_flags=['readwrite']):
        print(x[...], end=',')
        x[...] = 2 * x
    print('修改后的数组是:\n', a)


def test_flags():
    a = np.arange(0, 60, 5)
    a = a.reshape(3, 4)
    print("原数组\n", a)
    print('迭代数组')
    for x in np.nditer(a, flags=['external_loop'], order='F'):
        print(x, '---')

    print('\nc_index')
    for x in np.nditer(a, flags=['c_index'], order='F'):
        print(x, '---')

    print('\nf_index')
    for x in np.nditer(a, flags=['f_index'], order='F'):
        print(x, '---')


def test_iter_broad():
    a = np.arange(0, 60, 5)
    a = a.reshape(3, 4)
    print(a)
    b = np.array([1, 2, 3, 4], dtype=int)
    print(b)
    # 广播迭代
    for x, y in np.nditer([a, b]):
        print(f_'{x:<3}=>{y:>3}', end=", ")


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    runTest(test_broad)
    runTest(test_broad2)
    runTest(test_iter)
    runTest(test_iter_order)
    runTest(test_op_flags)
    runTest(test_flags)
    runTest(test_iter_broad)
